﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Board.Models
{
    public class Unit
    {
        private bool _exist = false;

        private HashSet<Bed> _beds = null;
        private HashSet<Bed> _accessibleBeds = null; 

        public static string ALL_UNITS = "All_Units";

        public string SiteId { get; set; }

        public string UnitId { get; set; }


        public HashSet<Bed> Beds
        {
            get
            {
                if (_beds == null)
                {
                    using (DbEntities db = new DbEntities())
                    {
                        _beds= new HashSet<Bed>((from u in db.t_AllUnitBed
                                                 where u.SiteID.Equals(SiteId) && u.UnitID.Equals(UnitId)
                                                 select new Bed() {SiteId = u.SiteID, UnitId = u.UnitID, BedId = u.Bed}));
                    }
                }
                return _beds;
            }
            set { _beds = value; }
        }

        public HashSet<Bed> AccessibleBeds
        {
            get
            {
                return _accessibleBeds;
            }
            set { _accessibleBeds = value; }
        } 

        public bool Exist
        {
            get
            {
                if (!_exist && !string.IsNullOrEmpty(SiteId + UnitId))
                {
                    using (DbEntities db = new DbEntities())
                    {
                        _exist = (from u in db.t_AllUnitBed
                                  where u.SiteID.Equals(SiteId) && u.UnitID.Equals(UnitId)
                                  select u).Any();
                    }
                }
                return _exist;
            }
        }

        public Unit(string siteId, string unitId)
        {
            using (DbEntities db = new DbEntities())
            {
                if ((from u in db.t_AllUnitBed where u.SiteID.Equals(siteId) && u.UnitID.Equals(unitId) select u).Any())
                {
                    SiteId = siteId;
                    UnitId = unitId;
                    _exist = true;
                }
                else
                {
                    _exist = false;
                }
            }
        }
        public Unit()
        {
        }

        public bool Equals(Unit unit)
        {
            if (!string.IsNullOrEmpty(SiteId) && !string.IsNullOrEmpty(UnitId) && SiteId.Equals(unit.SiteId) &&
                UnitId.Equals(unit.UnitId))
            {
                return Exist;
            }
            return false;
        }







    }
}